/*Copyright (C) 2009 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Input.h"
#include "System.h"
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#ifdef WIN32
# include <windows.h>
#else /* WIN32 */
# include <termios.h>
#endif /* !WIN32 */

//fonction externe
///////////////////////////////////////////////////////////
/// Permet d'attendre que l'utilisateur presse une touche
///////////////////////////////////////////////////////////
int DLL_IMPEXP CSLP_PollChar()
{
  int retVal = 0;
#ifdef WIN32
  if (kbhit() != 0)
    {
      //touche appuyée et prette à être récupérée
      retVal = 1;
    }
#else
  //initialisation des varaibles
  struct timeval sTime = {0, 0};
  fd_set readfds;
  FD_ZERO(&readfds);
  FD_SET(0, &readfds);
  retVal = select(1, &readfds, NULL, NULL, &sTime);
      //retVal = -1 	-> error
      //retVal = 0 	-> timeout
      //retVal > 0	-> event
#endif

  return retVal;
}

//fonction externe
//
///////////////////////////////////////////////////////////
/// Permet de récupérer la touche pressé par l'utilisateur
///////////////////////////////////////////////////////////
int DLL_IMPEXP CSLP_GetChar()
{
  int retVal = 0;
  if (CSLP_PollChar() > 0)
    {
      retVal = getch();
    }
  return retVal;
}
